package edu.msse.signup.ui;

import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import javax.faces.application.FacesMessage;
import java.io.IOException;
import java.util.ArrayList;

import edu.msse.signup.service.UsersService;
import edu.msse.signup.service.TimeSlotService;
import edu.msse.signup.service.PositionService;
import edu.msse.signup.domain.TimeSlot;
import edu.msse.signup.domain.Position;

/**
 * Created by IntelliJ IDEA.
 * User: shenry
 * Date: Apr 20, 2008
 * Time: 5:37:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class VolunteerDetailsServlet extends HttpServlet {
    WebApplicationContext spring;
    private static final long serialVersionUID = -2560989072138239258L;

    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        spring = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UsersService userService = (UsersService) spring.getBean("userService");
        TimeSlotService timeslotService = (TimeSlotService) spring.getBean("timeSlotService");
        PositionService positionService = (PositionService) spring.getBean("positionService");
       try{
            String positionId = request.getParameter("positionId");
            String timeSlotId = request.getParameter("timeslotId");
           TimeSlot timeslot = null;
           if (timeSlotId!= null)  {
               timeslot = timeslotService.getTimeSlot(Long.valueOf(timeSlotId));
           }
           Position position = positionService.getPosition(Integer.valueOf(positionId));

           request.setAttribute("position", position);
           request.setAttribute("timeslot", timeslot);

       }catch(Exception e){
           e.printStackTrace();
           String error ="An error has occurred: " + e.getMessage();
           request.setAttribute("error", error);

       }

            RequestDispatcher dispatcher = request.getRequestDispatcher("/volunteerDetails.jsp");
			dispatcher.forward(request, response);
    }
}